/*
http://www.netfor2.com/dns.htm
http://www.koders.com/c/fidEC46CF755920D7463FA1B6F0DC2C8FAA6A920B9A.aspx?s=gethostbyname2
http://www.unix.org.ua/orelly/networking/dnsbind/ch14_02.htm

RFC 1035

DNS MESSAGE FORMAT

   +---------------------+
   |        Header       |
   +---------------------+
   |       Question      | the question for the name server
   +---------------------+
   |        Answer       | RRs answering the question
   +---------------------+
   |      Authority      | RRs pointing toward an authority
   +---------------------+
   |      Additional     | RRs holding additional information
   +---------------------+

DNS HEADER FORMAT

                                    1  1  1  1  1  1
      0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                      ID                       |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |QR|   Opcode  |AA|TC|RD|RA|   Z    |   RCODE   |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                    QDCOUNT                    |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                    ANCOUNT                    |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                    NSCOUNT                    |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                    ARCOUNT                    |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

where:

ID              A 16 bit identifier assigned by the program that
                generates any kind of query.  This identifier is copied
                the corresponding reply and can be used by the requester
                to match up replies to outstanding queries.

QR              A one bit field that specifies whether this message is a
                query (0), or a response (1).

OPCODE          A four bit field that specifies kind of query in this
                message.  This value is set by the originator of a query
                and copied into the response.  The values are:

                0               a standard query (QUERY)

                1               an inverse query (IQUERY)

                2               a server status request (STATUS)

                3-15            reserved for future use

AA              Authoritative Answer - this bit is valid in responses,
                and specifies that the responding name server is an
                authority for the domain name in question section.

                Note that the contents of the answer section may have
                multiple owner names because of aliases.  The AA bit
                corresponds to the name which matches the query name, or
                the first owner name in the answer section.

TC              TrunCation - specifies that this message was truncated
                due to length greater than that permitted on the
                transmission channel.

RD              Recursion Desired - this bit may be set in a query and
                is copied into the response.  If RD is set, it directs
                the name server to pursue the query recursively.
                Recursive query support is optional.

RA              Recursion Available - this be is set or cleared in a
                response, and denotes whether recursive query support is
                available in the name server.

Z               Reserved for future use.  Must be zero in all queries
                and responses.

RCODE           Response code - this 4 bit field is set as part of
                responses.  The values have the following
                interpretation:

                0               No error condition

                1               Format error - The name server was
                                unable to interpret the query.

                2               Server failure - The name server was
                                unable to process this query due to a
                                problem with the name server.

                3               Name Error - Meaningful only for
                                responses from an authoritative name
                                server, this code signifies that the
                                domain name referenced in the query does
                                not exist.

                4               Not Implemented - The name server does
                                not support the requested kind of query.

                5               Refused - The name server refuses to
                                perform the specified operation for
                                policy reasons.  For example, a name
                                server may not wish to provide the
                                information to the particular requester,
                                or a name server may not wish to perform
                                a particular operation (e.g., zone
                                transfer) for particular data.

                6-15            Reserved for future use.

QDCOUNT         an unsigned 16 bit integer specifying the number of
                entries in the question section.

ANCOUNT         an unsigned 16 bit integer specifying the number of
                resource records in the answer section.

NSCOUNT         an unsigned 16 bit integer specifying the number of name
                server resource records in the authority records
                section.

ARCOUNT         an unsigned 16 bit integer specifying the number of
                resource records in the additional records section.


QUESTION FORMAT

OCTET 1,2,?n 	QNAME 
OCTET n+1,n+2	QTYPE
OCTET n+3,n+4	QCLASS 

ANSWER, AUTHORITY, ADDITIONAL FORMAT

OCTET 1,2,..n 		NAME 
OCTET n+1,n+2		TYPE
OCTET n+3,n+4		CLASS 
OCTET n+5,n+6,n+7,n+8	TTL 
OCTET n+9,n+10		RDLENGTH
OCTET n+11,n+12,?..	RDATA

domain name storage
www.piowave.com
3 www  7 piowave 3 com 0
*/

#if 0
TODO............

typedef struct xp_dnspkthdr_t;
#pragma pack(1)
struct xp_dnspkthdr_t
{
	rb_uint16_t qname; 	
	
	/* 0 query, 1 response */
	rb_uint16_t qr: 1; 
	/* 0 standard query, 1 inverse query, 
	 * 2 server status request, 3-15 reserved */
	rb_uint16_t opcode: 4; 
	rb_uint16_t aa: 1; /* authoritative answer */ 	
	rb_uint16_t tc: 1; /* truncation */
	rb_uint16_t rd: 1; /* recursion desired */
	rb_uint16_t ra: 1; /* resursion available */
	rb_uint16_t z: 3; /* reserved */
	rb_uint16_t rcode: 4; /* response code */
	
	rb_uint16_t qdcount; 	
	rb_uint16_t ancount; 	
	rb_uint16_t nscount; 	
	rb_uint16_t arcount; 	
};

#pragma pack(0)

xp_size_t xp_strdns (const xp_char_t* name, xp_byte_t* buf, xp_size_t size)
{
	const xp_char_t* sp, * ep;
	xp_mchar_t* bp = buf;
	
// TODO: utf conversion
	while (*name != XP_CHAR('\0')) {
		sp = name; ep = name; 
		while (*ep != XP_CHAR('\0') && *ep != XP_CHAR('.')) ep++;
		if (ep - sp > 63) return 0;
		
	   *bp++ = ep - sp;
	   while (sp < ep) *bp++= *sp++;
	}
	
	*bp = 0;
}

int make_dns_query (int op, const xp_char_t* dname, int class, int type)
{
	xp_byte_t buf[512];
	xp_dnspkthdr_t* hdr = (xp_dnspkthdr_t*)buf;
	const xp_char_t* p = dname;
	const xp_byte_t* bp;
	
	xp_memset (hdr, 0, XP_SIZEOF(xp_dnspkthdr_t));
	hdr->opcode = 0x0001;
	hdr->qr = 0; /* query */
	hdr->opcode = 0; /* standard query */
	hdr->tc = 1;
	hdr->qdcount = 1;
	
	bp = (xp_byte_t*)(hdr + 1);
	
	xp_memcpy (&bp, 
}

#endif
